/*
 * board_init.c
 *
 *  Created on: Aug 1, 2013
 *      Author: Ken Arok
 *
 * \brief Low Level Board Initialization.
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */

#include "config_board.h"

#if BOARD_1_0_USED

#include "board.h"
#include "gpio.h"


/* \brief LED I/Os initialization
 */
static void led_gpio_init(void)
{
	// Initialize Heart Beat LED
	gpio_enable_module_pin(LED_HB_PIN, LED_HB_FUNCTION);
	// Initialize Signal LED
	gpio_configure_pin(LED1_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
}


/* \brief Buzzer I/O initialization
 */
static void buzzer_gpio_init(void)
{
	gpio_enable_module_pin(BUZZER_PIN, BUZZER_FUNCTION);
}


/* \brief LCD Monochrome I/O initialization
 */
static void lcd_gpio_init(void)
{
	gpio_enable_module_pin(LCD_SPI_NPCS_PIN, LCD_SPI_NPCS_FUNCTION);

	// Assign I/O to LCD control pin
	gpio_configure_pin(LCD_E_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);
	gpio_configure_pin(LCD_BLON_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(LCD_WR_RW_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);
	gpio_configure_pin(LCD_A0_RS_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);
	gpio_configure_pin(LCD_CS_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);
	gpio_configure_pin(LCD_RESET_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);


}


/* \brief LCD TFT I/O initialization
 */
static void tft_gpio_init(void)
{
	// Assign I/O to TFT control pin
	gpio_enable_module_pin(TFT_BLON_PIN, TFT_BLON_FUNCTION);

	gpio_configure_pin(TFT_NCS_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(TFT_RD_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(TFT_WR_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(TFT_DNC_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(TFT_RESET_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);

}


/* \brief Key I/O Initialization
 */
static void key_gpio_init(void)
{
	static const gpio_map_t KEY_SPI_GPIO_MAP = {
		{KEY_SPI_NPCS_WR_PIN, KEY_SPI_NPCS_WR_FUNCTION},
		{KEY_SPI_NPCS_RD_PIN, KEY_SPI_NPCS_RD_FUNCTION},
	};

	// Assign I/Os to SPI.
	gpio_enable_module(KEY_SPI_GPIO_MAP, sizeof(KEY_SPI_GPIO_MAP) / sizeof(KEY_SPI_GPIO_MAP[0]));

	// Assign I/O to Key load pin
	gpio_configure_pin(KEY_LOAD_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);

}


/* \brief SD/MMC I/O Initialization
 */
static void sd_mmc_gpio_init(void)
{
	gpio_enable_module_pin(SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION);

	// Assign I/O to Write Protect pin
	gpio_configure_pin(SD_MMC_WP_GPIO, GPIO_DIR_INPUT);

	// Assign I/O to Card Detection pin
	gpio_configure_pin(SD_MMC_CD_GPIO, GPIO_DIR_INPUT);

}


/* \brief DATA FLASH/EEPROM I/O Initialization
 */
static void data_flash_gpio_init(void)
{
	gpio_enable_module_pin(DATAFLASH_SPI_NPCS_PIN, DATAFLASH_SPI_NPCS_FUNCTION);
}


/* \brief SPI0 I/O Initialization
 */
static void spi0_gpio_init(void) {

	static const gpio_map_t DEFAULT_SPI_GPIO_MAP = {
		{DEFAULT_SPI0_SCK_PIN, DEFAULT_SPI0_SCK_FUNCTION},
		{DEFAULT_SPI0_MOSI_PIN, DEFAULT_SPI0_MOSI_FUNCTION},
		{DEFAULT_SPI0_MISO_PIN, DEFAULT_SPI0_MISO_FUNCTION},
	};

	// Assign I/Os to SPI.
	gpio_enable_module(DEFAULT_SPI_GPIO_MAP, sizeof(DEFAULT_SPI_GPIO_MAP) / sizeof(DEFAULT_SPI_GPIO_MAP[0]));
}


/* \brief SPI1 I/O Initialization
 */
static void spi1_gpio_init(void) {

	static const gpio_map_t DEFAULT_SPI_GPIO_MAP = {
		{DEFAULT_SPI1_SCK_PIN, DEFAULT_SPI1_SCK_FUNCTION},
		{DEFAULT_SPI1_MOSI_PIN, DEFAULT_SPI1_MOSI_FUNCTION},
		{DEFAULT_SPI1_MISO_PIN, DEFAULT_SPI1_MISO_FUNCTION},
	};

	// Assign I/Os to SPI.
	gpio_enable_module(DEFAULT_SPI_GPIO_MAP, sizeof(DEFAULT_SPI_GPIO_MAP) / sizeof(DEFAULT_SPI_GPIO_MAP[0]));
}


/* \brief TWI0 I/O Initialization
 */
static void twims0_gpio_init(void) {

	static const gpio_map_t DEFAULT_TWIMS0_GPIO_MAP = {
		{DEFAULT_TWIMS0_TWCK_PIN, DEFAULT_TWIMS0_TWCK_FUNCTION},
		{DEFAULT_TWIMS0_TWD_PIN, DEFAULT_TWIMS0_TWD_FUNCTION},
		{DEFAULT_TWIMS0_TWALM_PIN, DEFAULT_TWIMS0_TWALM_PIN},
	};

	gpio_enable_module(DEFAULT_TWIMS0_GPIO_MAP, sizeof(DEFAULT_TWIMS0_GPIO_MAP) / sizeof(DEFAULT_TWIMS0_GPIO_MAP[0]));

}


/* \brief EIC I/O Initialization
 */
static void eic_gpio_init(void) {

	// Map the interrupt line to the GPIO pin
	gpio_enable_module_pin(KEY_INT_PIN, KEY_INT_FUNCTION);
}


/* \brief MACB I/O Initialization
 */
static void macb_gpio_init(void) {
	static const gpio_map_t MACB_GPIO_MAP =
	{
		{EXTPHY_MACB_MDC_PIN,     EXTPHY_MACB_MDC_FUNCTION   },
		{EXTPHY_MACB_MDIO_PIN,    EXTPHY_MACB_MDIO_FUNCTION  },
		{EXTPHY_MACB_RXD_0_PIN,   EXTPHY_MACB_RXD_0_FUNCTION },
		{EXTPHY_MACB_TXD_0_PIN,   EXTPHY_MACB_TXD_0_FUNCTION },
		{EXTPHY_MACB_RXD_1_PIN,   EXTPHY_MACB_RXD_1_FUNCTION },
		{EXTPHY_MACB_TXD_1_PIN,   EXTPHY_MACB_TXD_1_FUNCTION },
		{EXTPHY_MACB_TX_EN_PIN,   EXTPHY_MACB_TX_EN_FUNCTION },
		{EXTPHY_MACB_RX_ER_PIN,   EXTPHY_MACB_RX_ER_FUNCTION },
		{EXTPHY_MACB_RX_DV_PIN,   EXTPHY_MACB_RX_DV_FUNCTION },
		{EXTPHY_MACB_TX_CLK_PIN,  EXTPHY_MACB_TX_CLK_FUNCTION}
	};

	// Assign GPIO to MACB
	gpio_enable_module(MACB_GPIO_MAP, sizeof(MACB_GPIO_MAP) / sizeof(MACB_GPIO_MAP[0]));

	gpio_configure_pin(EXTPHY_MACB_RESET_PIN, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);
}


/* \brief USART I/O Initialization
 */
static void usart_gpio_init(void)
{
	static const gpio_map_t USART_GPIO_MAP =
	{
		{USART1_RXD_PIN, USART1_RXD_FUNCTION},
		{USART1_TXD_PIN, USART1_TXD_FUNCTION},
		{USART2_RXD_PIN, USART2_RXD_FUNCTION},
		{USART2_TXD_PIN, USART2_TXD_FUNCTION},
		{USART2_RTS_PIN, USART2_RTS_FUNCTION},
		{USART3_RXD_PIN, USART3_RXD_FUNCTION},
		{USART4_RXD_PIN, USART4_RXD_FUNCTION},
		{USART4_TXD_PIN, USART4_TXD_FUNCTION}
	};

	// Assign GPIO to USART
	gpio_enable_module(USART_GPIO_MAP, sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));

	gpio_enable_module_pin(USART_SPI_NPCS_PIN, USART_SPI_NPCS_FUNCTION);

	gpio_configure_pin(USART_CTRL_PIN_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_HIGH);
}


/* \brief External I/O Initialization
 */
static void ext_gpio_init(void)
{
	// default all pins are configured as output
	gpio_configure_pin(EXT_IO1_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO2_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO3_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO4_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO5_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO6_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO7_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO8_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO9_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO10_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(EXT_IO11_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
}


/* \brief External SPI1 I/O Initialization
 */
static void ext_spi1_gpio_init(void)
{
	// Assign GPIO to External SPI1
	gpio_enable_module_pin(EXT_SPI_NPCS_PIN, EXT_SPI_NPCS_FUNCTION);
}


/* \brief JTAG I/O Initialization
 */
static void jtag_gpio_init(void)
{
	// Default is unused JTAG. Thus all pin must be configure as output
	gpio_configure_pin(TCK_PIN_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(TDI_PIN_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	//gpio_configure_pin(TDO_PIN_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(TDO_PIN_GPIO, GPIO_DIR_INPUT);
	gpio_configure_pin(TMS_PIN_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
}


/* \brief Unused I/O Initialization
 */
static void unused_gpio_init(void)
{
	gpio_configure_pin(UNUSED_PIN1_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(UNUSED_PIN2_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(UNUSED_PIN3_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);
	gpio_configure_pin(UNUSED_PIN4_GPIO, GPIO_DIR_OUTPUT | GPIO_INIT_LOW);

}


void board_init(void)
{
	/* initialize unused GPIO */
	unused_gpio_init();

	/* initialize jtag GPIO */
	jtag_gpio_init();

	/* initialize external I/O GPIO */
	ext_gpio_init();

	/* initialize external SPI1 GPIO */
	ext_spi1_gpio_init();

	/* initialize LED GPIO */
	led_gpio_init();

	/* Initialize BUZZER GPIO */
	buzzer_gpio_init();

	/* initialize TFT GPIO */
	tft_gpio_init();

	/* Initialize LCD GPIO */
	lcd_gpio_init();

	/* Initialize KEYPAD GPIO */
	key_gpio_init();

	/* Initialize SPI0 GPIO */
	spi0_gpio_init();

	/* Initialize SPI1 GPIO */
	spi1_gpio_init();

	/* Initialize TWI GPIO */
	twims0_gpio_init();

	/* Initialize EIC GPIO */
	eic_gpio_init();

	/* Initialize MACB GPIO */
	macb_gpio_init();

	/* Initialize SD/MMC GPIO */
	sd_mmc_gpio_init();

	/* Initialize Data Flash GPIO */
	data_flash_gpio_init();

	/* Initialize USART GPIO */
	usart_gpio_init();

}

#endif /* BOARD_1_0_USED */
